package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.MinIoTemplate;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.thread.UserThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.sound.midi.SysexMessage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
    @Autowired
    private MinIoTemplate minIoTemplate;
    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    @Override
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        //1.获得上传图片的输入流
        InputStream inputStream = multipartFile.getInputStream();
        String contentType = multipartFile.getContentType();
        if(contentType.isEmpty()){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //后缀名
        String[] split = contentType.split("/");
        String suffixName = split[1];
        //文件名
        String fileName=System.currentTimeMillis() + "."+ suffixName;
        //2.把图片上传到minio
        String url = minIoTemplate.uploadFile("",fileName , inputStream, "image");
        //3.将上传后的url,写入到数据库material这张表
        WmMaterial material = new WmMaterial();
        material.setCreatedTime(new Date());
        material.setId(UserThreadLocal.get().getId());
        material.setIsCollection((short) 0);//0,默认不收藏
        material.setType((short) 0);//0,默认图片
        material.setUrl(url);
        material.setUserId(0);
        int insertResult = wmMaterialMapper.insert(material);
        if (insertResult<1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        //4.创建好的素材信息,返回给前端
        return ResponseResult.okResult(material);
    }

    @Override
    public PageResponseResult list(WmMaterialDto dto) {
        if (dto==null) {
            return new PageResponseResult(0,0,0);
        }
        //1.构建查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getIsCollection()==1,WmMaterial::getIsCollection,dto.getIsCollection());//1,为收藏
        //2.构建分页条件
        Integer dtoPage = dto.getPage();
        Integer dtoSize = dto.getSize();
        Page<WmMaterial> page = new Page<>(dtoPage,dtoSize);
        //3.查询
        Page<WmMaterial> pageResult = wmMaterialMapper.selectPage(page, wrapper);
        //4.组装PageResonseResult并返回
        long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(dtoPage, dtoSize, (int) total);
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }
}
